﻿using DoNet.Common.Pages;
using DoNet.Core.Dtos;
using DoNet.ThirdParty.AutoMapper;
using SqlSugar;
using System.Linq.Expressions;

namespace DoNet.Core
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="TODto"></typeparam>
    public class BaseService<T, TODto> : IService<T, TODto> where T : class, new() where TODto : class
    {
        /// <summary>
        /// 通过在子类的构造函数中注入，这里是基类，不用构造函数
        /// </summary>
        public required IRepository<T> baseRepository { get; set; }

        #region 同步方法
        #region 同步新增
        /// <summary>
        /// 同步新增实体。
        /// </summary>
        /// <param name="entitie">实体</param>
        /// <returns></returns>
        public virtual int Insert(T entitie)
        {
            return baseRepository.Insert(entitie);
        }
        /// <summary>
        /// 同步批量新增实体。
        /// </summary>
        /// <param name="entities">实体集合</param>
        /// <returns></returns>
        public virtual int Insert(List<T> entities)
        {
            return baseRepository.Insert(entities);
        }
        /// <summary>
        /// 同步新增或更新
        /// </summary>
        /// <param name="entities"></param>
        /// <returns>(返回新增数量, 返回更新数量)</returns>
        public virtual Tuple<int, int> InsertOrUpdate(List<T> entities)
        {
            return baseRepository.InsertOrUpdate(entities);
        }
        #endregion 同步新增

        #region 同步修改
        /// <summary>
        /// 同步按条件更新
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="whereSql">条件语句 如："Age > @Age"</param>
        /// <param name="parameters">条件参数 如：new { Age = 30 }</param>
        /// <returns>执行成功返回true，否则为false。</returns>
        public virtual bool Update(T entity, string whereSql = "", object? parameters = null)
        {
            return baseRepository.Update(entity, whereSql, parameters);
        }
        /// <summary>
        /// 同步按条件更新指定字段
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="listColumns">需要更新的字段</param>
        /// <param name="listIgnoreColumns">不用更新的字段</param>
        /// <returns>执行成功返回true，否则为false。</returns>
        public virtual bool Update(T entity, IEnumerable<string> listColumns, IEnumerable<string> listIgnoreColumns)
        {
            return baseRepository.Update(entity, listColumns, listIgnoreColumns);
        }
        /// <summary>
        /// 同步按条件更新指定字段
        /// </summary>
        /// <param name="entitys"></param>
        /// <param name="listColumns">需要更新的字段</param>
        /// <param name="listIgnoreColumns">不用更新的字段</param>
        /// <param name="whereSql">条件语句 如："Age > @Age"</param>
        /// <param name="parameters">条件参数 如：new { Age = 30 }</param>
        /// <returns>执行成功返回true，否则为false。</returns>
        public virtual bool Update(IEnumerable<T> entitys, IEnumerable<string> listColumns, IEnumerable<string> listIgnoreColumns, string whereSql = "", object? parameters = null)
        {
            return baseRepository.Update(entitys, listColumns, listIgnoreColumns, whereSql, parameters);
        }
        /// <summary>
        /// 异步根据id更新
        /// </summary>
        /// <param name="columns">更新的列及值</param>
        /// <param name="id"></param>
        /// <returns>执行成功返回true，否则为false。</returns>
        public virtual bool Update(Expression<Func<T, T>> columns, long id)
        {
            return baseRepository.Update(columns, id);
        }
        /// <summary>
        /// 同步按条件更新（条件语句为空或者条件参数为null时，就代表没有条件，因此使用此方法需谨慎）
        /// </summary>
        /// <param name="columns">更新的列及值</param>
        /// <param name="whereSql">条件语句 如："Age > @Age"</param>
        /// <param name="parameters">条件参数 如：new { Age = 30 }</param>
        /// <returns>执行成功返回true，否则为false。</returns>
        public virtual bool Update(Expression<Func<T, T>> columns, string whereSql = "", object? parameters = null)
        {
            return baseRepository.Update(columns, whereSql, parameters);
        }
        #endregion 同步修改

        #region 同步查询
        #region 单个实体
        /// <summary>
        /// 同步根据主键查询单个实体。
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public virtual T GetById(long id)
        {
            return baseRepository.GetById(id);
        }
        /// <summary>
        /// 同步根据条件获取一个对象
        /// </summary>
        /// <param name="whereSql">条件语句 如："Age=@Age"</param>
        /// <param name="parameters">条件参数 如：new { Age=30 }</param>
        /// <returns></returns>
        public virtual T? GetByWhere(string whereSql, object parameters)
        {
            return baseRepository.GetByWhere(whereSql, parameters);
        }
        /// <summary>
        /// 同步根据条件获取一个对象
        /// 用于返回查询结果的第一条记录，但如果查询结果为空，它将返回 default(T) 值（通常是 null 对于引用类型）
        /// </summary>
        /// <param name="whereExpression">查询条件表达式</param>
        /// <returns></returns>
        public virtual T GetFirstByWhere(Expression<Func<T, bool>> whereExpression)
        {
            return baseRepository.GetFirstByWhere(whereExpression);
        }
        /// <summary>
        /// 同步根据条件获取一个对象
        /// 用于返回一个单一的结果，如果查询结果有多于一个结果，它会抛出一个异常（通常是 InvalidOperationException)
        /// </summary>
        /// <param name="whereExpression">查询条件表达式</param>
        /// <returns></returns>
        public virtual T GetSingleByWhere(Expression<Func<T, bool>> whereExpression)
        {
            return baseRepository.GetSingleByWhere(whereExpression);
        }
        /// <summary>
        /// 同步查询单个实体。
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public virtual TODto? GetOutDto(long id)
        {
            return baseRepository.GetById(id).MapTo<TODto>();
        }
        #endregion 单个实体

        #region 实体列表
        /// <summary>
        /// 同步查询所有实体。
        /// </summary>        
        /// <returns></returns>
        public virtual IEnumerable<T> GetAll()
        {
            return baseRepository.GetAll();
        }
        /// <summary>
        /// 同步根据查询条件获取数据集合
        /// </summary>
        /// <param name="whereSql">条件语句 如："Age=@Age"</param>
        /// <param name="parameters">条件参数 如：new { Age=30 }</param>
        /// <returns></returns>
        public virtual IEnumerable<T>? GetListByWhere(string whereSql = "", object? parameters = null)
        {
            return baseRepository.GetListByWhere(whereSql, parameters);
        }
        /// <summary>
        /// 同步根据查询条件获取数据集合
        /// </summary>
        /// <param name="whereExpression">查询条件表达式</param>
        /// <returns></returns>
        public virtual IEnumerable<T> GetListByWhere(Expression<Func<T, bool>> whereExpression)
        {
            return baseRepository.GetListByWhere(whereExpression);
        }
        /// <summary>
        /// 同步根据查询条件查询前多少条数据
        /// SQL语句，仅支持SqlServer和Mysql
        /// </summary>
        /// <param name="top">多少条数据</param>
        /// <param name="whereExpression">查询条件表达式</param>
        /// <returns></returns>
        public virtual IEnumerable<T> GetListTopWhere(int top, Expression<Func<T, bool>> whereExpression)
        {
            return baseRepository.GetListTopWhere(top, whereExpression);
        }
        /// <summary>
        /// 同步根据查询条件查询前多少条数据
        /// SQL语句，仅支持SqlServer和Mysql
        /// </summary>
        /// <param name="top">多少条数据</param>
        /// <param name="whereSql">条件语句 如："Age=@Age"</param>
        /// <param name="parameters">条件参数 如：new { Age=30 }</param>
        /// <returns></returns>
        public virtual IEnumerable<T>? GetListTopWhere(int top, string whereSql, object parameters)
        {
            return baseRepository.GetListTopWhere(top, whereSql, parameters);
        }
        /// <summary>
        /// 同步查询对应有效状态的数据
        /// </summary>
        /// <param name="enabledMark">有效状态</param>
        /// <returns></returns>
        public virtual IEnumerable<T>? GetAllByEnabledMark(bool enabledMark)
        {
            return GetListByWhere("EnabledMark=@EnabledMark", new { EnabledMark = enabledMark });
        }
        /// <summary>
        /// 同步查询软删除状态的数据
        /// </summary>
        /// <param name="deleteMark">软删除状态</param>
        /// <returns></returns>
        public virtual IEnumerable<T>? GetAllByDeleteMark(bool deleteMark)
        {
            return GetListByWhere("DeleteMark=@DeleteMark", new { DeleteMark = deleteMark });
        }
        /// <summary>
        /// 同步查询未软删除且有效的数据，如果查询条件为空，即查询所有数据
        /// </summary>
        /// <param name="deleteMark">软删除状态</param>
        /// <param name="enabledMark">有效状态</param>
        /// <returns></returns>
        public virtual IEnumerable<T>? GetAllByDeleteMarkAndEnabledMark(bool deleteMark, bool enabledMark)
        {
            return GetListByWhere("DeleteMark=@DeleteMark and EnabledMark=@EnabledMark", new { DeleteMark = deleteMark, EnabledMark = enabledMark });
        }
        #endregion 实体列表

        #region 分页实体列表
        /// <summary>
        /// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
        /// </summary>
        /// <param name="condition">查询的条件</param>
        /// <param name="info">分页实体</param>
        /// <returns>指定对象的集合</returns>
        public virtual List<T> FindWithPager(string condition, PagerInfo info)
        {
            return baseRepository.FindWithPager(condition, info);
        }
        /// <summary>
        /// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
        /// </summary>
        /// <param name="condition">查询的条件</param>
        /// <param name="info">分页实体</param>
        /// <param name="fieldToSort">排序字段</param>
        /// <returns>指定对象的集合</returns>
        public virtual List<T> FindWithPager(string condition, PagerInfo info, string fieldToSort)
        {
            return baseRepository.FindWithPager(condition, info, fieldToSort);
        }
        /// <summary>
        /// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
        /// </summary>
        /// <param name="condition">查询的条件</param>
        /// <param name="info">分页实体</param>
        /// <param name="fieldToSort">排序字段</param>
        /// <param name="desc">是否降序</param>
        /// <returns>指定对象的集合</returns>
        public virtual List<T> FindWithPager(string condition, PagerInfo info, string fieldToSort, bool desc)
        {
            return baseRepository.FindWithPager(condition, info, fieldToSort, desc);
        }
        /// <summary>
        /// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
        /// 查询条件变换时请重写该方法。
        /// </summary>
        /// <param name="search">查询的条件</param>
        /// <returns>指定对象的集合</returns>
        public virtual PageResult<TODto> FindWithPager(SearchInputDto<T> search)
        {
            bool order = search.Order == "asc" ? false : true;
            PagerInfo pagerInfo = new PagerInfo
            {
                CurrenetPageIndex = search.CurrenetPageIndex,
                PageSize = search.PageSize
            };
            string where = string.Empty;
            List<T> list = baseRepository.FindWithPager(where, pagerInfo, search.Sort, order);
            PageResult<TODto> pageResult = new PageResult<TODto>
            {
                CurrentPage = pagerInfo.CurrenetPageIndex,
                Items = list.MapTo<TODto>(),
                ItemsPerPage = pagerInfo.PageSize,
                TotalItems = pagerInfo.RecordCount
            };
            return pageResult;
        }
        #endregion 分页实体列表

        #region Count、Max、Min、Sum
        /// <summary>
        /// 同步根据条件统计数据（sql语句方法）
        /// </summary>
        /// <param name="whereSql">条件语句 如："Age=@Age"</param>
        /// <param name="parameters">条件参数 如：new { Age=30 }</param>
        /// <returns></returns>
        public virtual int GetCountByWhere(string whereSql = "", object? parameters = null)
        {
            return baseRepository.GetCountByWhere(whereSql, parameters);
        }
        /// <summary>
        /// 同步根据条件统计数据（表达式方法）
        /// </summary>
        /// <param name="whereExpression">查询条件表达式</param>
        /// <returns></returns>
        public virtual int GetCountByWhere(Expression<Func<T, bool>> whereExpression)
        {
            return baseRepository.GetCountByWhere(whereExpression);
        }
        /// <summary>
        /// 同步根据条件查询获取某个字段的最大值（sql语句方法）
        /// </summary>
        /// <typeparam name="TResult">最大值的数据类型</typeparam>
        /// <param name="maxField">最大值的字段名称</param>
        /// <param name="whereSql">条件语句 如："Age=@Age"</param>
        /// <param name="parameters">条件参数 如：new { Age=30 }</param>
        /// <returns>返回字段的最小值</returns>
        public virtual TResult? GetMaxValueByField<TResult>(string maxField, string whereSql = "", object? parameters = null)
        {
            return baseRepository.GetMaxValueByField<TResult>(maxField, whereSql, parameters);
        }
        /// <summary>
        /// 同步根据条件查询获取某个字段的最大值（表达式方法）
        /// </summary>
        /// <typeparam name="TResult">最大值的数据类型</typeparam>
        /// <param name="maxFieldExpression">最大值字段表达式</param>
        /// <param name="whereExpression">查询条件表达式</param>
        /// <returns>返回字段的最小值</returns>
        public virtual TResult GetMaxValueByField<TResult>(Expression<Func<T, TResult>> maxFieldExpression, Expression<Func<T, bool>> whereExpression)
        {
            return baseRepository.GetMaxValueByField<TResult>(maxFieldExpression, whereExpression);
        }
        /// <summary>
        /// 同步根据条件查询获取某个字段的最小值（sql语句方法）
        /// </summary>
        /// <typeparam name="TResult">最小值的数据类型</typeparam>
        /// <param name="minField">最小值的字段名称</param>
        /// <param name="whereSql">条件语句 如："Age=@Age"</param>
        /// <param name="parameters">条件参数 如：new { Age=30 }</param>
        /// <returns>返回字段的最小值</returns>
        public virtual TResult? GetMinValueByField<TResult>(string minField, string whereSql = "", object? parameters = null)
        {
            return baseRepository.GetMinValueByField<TResult>(minField, whereSql, parameters);
        }
        /// <summary>
        /// 同步根据条件查询获取某个字段的最小值（表达式方法）
        /// </summary>
        /// <typeparam name="TResult">最小值的数据类型</typeparam>
        /// <param name="minFieldExpression">最小值字段表达式</param>
        /// <param name="whereExpression">查询条件表达式</param>
        /// <returns>返回字段的最小值</returns>
        public virtual TResult GetMinValueByField<TResult>(Expression<Func<T, TResult>> minFieldExpression, Expression<Func<T, bool>> whereExpression)
        {
            return baseRepository.GetMinValueByField<TResult>(minFieldExpression, whereExpression);
        }
        /// <summary>
        /// 同步根据条件统计某个字段之和,sum(字段)（sql语句方法）
        /// </summary>
        /// <typeparam name="TResult">求和后的数据类型</typeparam>
        /// <param name="sumField">求和的字段名称</param>
        /// <param name="whereSql">条件语句 如："Age=@Age"</param>
        /// <param name="parameters">条件参数 如：new { Age=30 }</param>
        /// <returns></returns>
        public virtual TResult? GetSumValueByField<TResult>(string sumField, string whereSql = "", object? parameters = null)
        {
            return baseRepository.GetSumValueByField<TResult>(sumField, whereSql, parameters);
        }
        /// <summary>
        /// 同步根据条件统计某个字段之和,sum(字段)（表达式方法）
        /// </summary>
        /// <typeparam name="TResult">求和后的数据类型</typeparam>
        /// <param name="sumFieldExpression">求和字段表达式</param>
        /// <param name="whereExpression">查询条件表达式</param>
        /// <returns></returns>
        public virtual TResult GetSumValueByField<TResult>(Expression<Func<T, TResult>> sumFieldExpression, Expression<Func<T, bool>> whereExpression)
        {
            return baseRepository.GetSumValueByField<TResult>(sumFieldExpression, whereExpression);
        }
        #endregion Count、Max、Min、Sum
        #endregion 同步查询

        #region 同步物理删除
        /// <summary>
        /// 同步按主键批量删除
        /// </summary>
        /// <param name="ids">主键Id List集合</param>
        /// <returns>执行成功返回true，否则为false。</returns>
        public virtual bool DeleteById(long id)
        {
            return baseRepository.DeleteById(id);
        }
        /// <summary>
        /// 同步按主键批量删除
        /// </summary>
        /// <param name="ids">主键Id List集合</param>
        /// <returns>执行成功返回true，否则为false。</returns>
        public virtual bool DeleteByIds(IEnumerable<long> ids)
        {
            return baseRepository.DeleteByIds(ids);
        }
        /// <summary>
        /// 异步按条件删除
        /// </summary>
        /// <param name="whereSql">条件语句 如："Age > @Age"</param>
        /// <param name="parameters">条件参数及值 如：new { Age = 30 }</param>
        /// <returns></returns>
        public virtual bool DeleteByWhere(string whereSql = "", object? parameters = null)
        {
            return baseRepository.DeleteByWhere(whereSql, parameters);
        }
        #endregion

        #region 事务
        /// <summary>
        /// 同步多表多数据操作批量插入、更新、删除--事务
        /// </summary>
        /// <param name="trans">事务</param>
        /// <param name="commandTimeout">超时</param>
        /// <returns></returns>
        public virtual Tuple<bool, string> ExecuteTransaction(List<Tuple<string, object?>> trans, int? commandTimeout = null)
        {
            return baseRepository.ExecuteTransaction(trans, commandTimeout);
        }
        /// <summary>
        /// 同步使用事务
        /// </summary>
        /// <param name="action">事务执行的方法</param>
        /// <returns></returns>
        public DbResult<bool> TransactionExecute(Action action)
        {
            return baseRepository.TransactionExecute(action);
        }
        #endregion 事务
        #endregion 同步方法

        #region 异步方法
        #region 异步新增
        /// <summary>
        /// 异步新增实体。
        /// </summary>
        /// <param name="entitie">实体</param>
        /// <returns></returns>
        public virtual async Task<int> InsertAsync(T entitie)
        {
            return await baseRepository.InsertAsync(entitie);
        }
        /// <summary>
        /// 异步批量新增实体。
        /// </summary>
        /// <param name="entities">实体集合</param>
        /// <returns></returns>
        public virtual async Task<int> InsertAsync(List<T> entities)
        {
            return await baseRepository.InsertAsync(entities);
        }
        /// <summary>
        /// 异步新增或更新
        /// </summary>
        /// <param name="entities"></param>
        /// <returns>(返回新增数量, 返回更新数量)</returns>
        public virtual async Task<Tuple<int, int>> InsertOrUpdateAsync(List<T> entities)
        {
            return await baseRepository.InsertOrUpdateAsync(entities);
        }
        #endregion 异步新增

        #region 异步修改
        /// <summary>
        /// 异步按条件更新
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="whereSql">条件语句 如："Age > @Age"</param>
        /// <param name="parameters">条件参数 如：new { Age = 30 }</param>
        /// <returns>执行成功返回true，否则为false。</returns>
        public virtual async Task<bool> UpdateAsync(T entity, string whereSql = "", object? parameters = null)
        {
            return await baseRepository.UpdateAsync(entity, whereSql, parameters);
        }
        /// <summary>
        /// 异步按条件更新指定字段
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="listColumns">需要更新的字段</param>
        /// <param name="listIgnoreColumns">不用更新的字段</param>
        /// <returns>执行成功返回true，否则为false。</returns>
        public virtual async Task<bool> UpdateAsync(T entity, IEnumerable<string> listColumns, IEnumerable<string> listIgnoreColumns)
        {
            return await baseRepository.UpdateAsync(entity, listColumns, listIgnoreColumns);
        }
        /// <summary>
        /// 异步按条件更新指定字段
        /// </summary>
        /// <param name="entitys"></param>
        /// <param name="listColumns">需要更新的字段</param>
        /// <param name="listIgnoreColumns">不用更新的字段</param>
        /// <param name="whereSql">条件语句 如："Age > @Age"</param>
        /// <param name="parameters">条件参数 如：new { Age = 30 }</param>
        /// <returns>执行成功返回true，否则为false。</returns>
        public virtual async Task<bool> UpdateAsync(IEnumerable<T> entitys, IEnumerable<string> listColumns, IEnumerable<string> listIgnoreColumns, string whereSql = "", object? parameters = null)
        {
            return await baseRepository.UpdateAsync(entitys, listColumns, listIgnoreColumns, whereSql, parameters);
        }
        /// <summary>
        /// 异步根据id更新
        /// </summary>
        /// <param name="columns">更新的列及值</param>
        /// <param name="id"></param>
        /// <returns>执行成功返回true，否则为false。</returns>
        public virtual async Task<bool> UpdateAsync(Expression<Func<T, T>> columns, long id)
        {
            return await baseRepository.UpdateAsync(columns, id);
        }
        /// <summary>
        /// 异步按条件更新（条件语句为空或者条件参数为null时，就代表没有条件，因此使用此方法需谨慎）
        /// </summary>
        /// <param name="columns">更新的列及值</param>
        /// <param name="whereSql">条件语句 如："Age > @Age"</param>
        /// <param name="parameters">条件参数 如：new { Age = 30 }</param>
        /// <returns>执行成功返回true，否则为false。</returns>
        public virtual async Task<bool> UpdateAsync(Expression<Func<T, T>> columns, string whereSql = "", object? parameters = null)
        {
            return await baseRepository.UpdateAsync(columns, whereSql, parameters);
        }
        #endregion 异步修改

        #region 异步查询
        #region 单个实体
        /// <summary>
        /// 异步根据主键查询单个实体。
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public virtual async Task<T> GetByIdAsync(long id)
        {
            return await baseRepository.GetByIdAsync(id);
        }
        /// <summary>
        /// 异步根据条件获取一个对象
        /// </summary>
        /// <param name="whereSql">条件语句 如："Age=@Age"</param>
        /// <param name="parameters">条件参数 如：new { Age=30 }</param>
        /// <returns></returns>
        public virtual async Task<T> GetByWhereAsync(string whereSql, object parameters)
        {
            return await baseRepository.GetByWhereAsync(whereSql, parameters);
        }
        /// <summary>
        /// 异步根据条件获取一个对象
        /// 用于返回查询结果的第一条记录，但如果查询结果为空，它将返回 default(T) 值（通常是 null 对于引用类型）
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        public virtual async Task<T> GetFirstByWhereAsync(Expression<Func<T, bool>> whereExpression)
        {
            return await baseRepository.GetFirstByWhereAsync(whereExpression);
        }
        /// <summary>
        /// 异步根据条件获取一个对象
        /// 用于返回一个单一的结果，如果查询结果有多于一个结果，它会抛出一个异常（通常是 InvalidOperationException)
        /// </summary>
        /// <param name="whereExpression">查询条件表达式</param>
        /// <returns></returns>
        public virtual async Task<T> GetSingleByWhereAsync(Expression<Func<T, bool>> whereExpression)
        {
            return await baseRepository.GetSingleByWhereAsync(whereExpression);
        }
        /// <summary>
        /// 异步查询单个实体。
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public virtual async Task<TODto?> GetOutDtoAsync(long id)
        {
            return (await GetByIdAsync(id)).MapTo<TODto>();
        }
        #endregion 单个实体

        #region 实体列表
        /// <summary>
        /// 异步获取所有数据，谨慎使用
        /// </summary>
        /// <returns></returns>
        public virtual async Task<IEnumerable<T>> GetAllAsync()
        {
            return await baseRepository.GetAllAsync();
        }
        /// <summary>
        /// 异步根据查询条件获取数据集合
        /// </summary>
        /// <param name="whereSql">条件语句 如："Age=@Age"</param>
        /// <param name="parameters">条件参数 如：new { Age=30 }</param>
        /// <returns></returns>
        public virtual async Task<IEnumerable<T>> GetListByWhereAsync(string whereSql, object parameters)
        {
            return await baseRepository.GetListByWhereAsync(whereSql, parameters);
        }
        /// <summary>
        /// 异步根据查询条件获取数据集合
        /// </summary>
        /// <param name="whereExpression">查询条件表达式</param>
        /// <returns></returns>
        public virtual async Task<IEnumerable<T>> GetListByWhereAsync(Expression<Func<T, bool>> whereExpression)
        {
            return await baseRepository.GetListByWhereAsync(whereExpression);
        }
        /// <summary>
        /// 异步根据查询条件查询前多少条数据
        /// SQL语句，仅支持SqlServer和Mysql
        /// </summary>
        /// <param name="top">多少条数据</param>
        /// <param name="whereExpression">查询条件表达式</param>
        /// <returns></returns>
        public virtual async Task<IEnumerable<T>> GetListTopWhereAsync(int top, Expression<Func<T, bool>> whereExpression)
        {
            return await baseRepository.GetListTopWhereAsync(top, whereExpression);
        }
        /// <summary>
        /// 异步根据查询条件查询前多少条数据
        /// SQL语句，仅支持SqlServer和Mysql
        /// </summary>
        /// <param name="top">多少条数据</param>
        /// <param name="whereSql">条件语句 如："Age=@Age"</param>
        /// <param name="parameters">条件参数 如：new { Age=30 }</param>
        /// <returns></returns>
        public virtual async Task<IEnumerable<T>> GetListTopWhereAsync(int top, string whereSql, object parameters)
        {
            return await baseRepository.GetListTopWhereAsync(top, whereSql, parameters);
        }
        /// <summary>
        /// 异步查询对应有效状态的数据
        /// </summary>
        /// <param name="enabledMark">有效状态</param>
        /// <returns></returns>
        public virtual async Task<IEnumerable<T>> GetAllByEnabledMarkAsync(bool enabledMark)
        {
            return await baseRepository.GetAllByEnabledMarkAsync(enabledMark);
        }
        /// <summary>
        /// 异步查询软删除状态的数据
        /// </summary>
        /// <param name="deleteMark">软删除状态</param>
        /// <returns></returns>
        public virtual async Task<IEnumerable<T>> GetAllByDeleteMarkAsync(bool deleteMark)
        {
            return await baseRepository.GetAllByDeleteMarkAsync(deleteMark);
        }
        /// <summary>
        /// 异步查询未软删除且有效的数据，如果查询条件为空，即查询所有数据
        /// </summary>
        /// <param name="deleteMark">软删除状态</param>
        /// <param name="enabledMark">有效状态</param>
        /// <returns></returns>
        public virtual async Task<IEnumerable<T>> GetAllByDeleteMarkAndEnabledMarkAsync(bool deleteMark, bool enabledMark)
        {
            return await baseRepository.GetAllByDeleteMarkAndEnabledMarkAsync(deleteMark, enabledMark);
        }
        #endregion 实体列表

        #region 分页实体列表
        /// <summary>
        /// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
        /// </summary>
        /// <param name="condition">查询的条件</param>
        /// <param name="info">分页实体</param>
        /// <returns>指定对象的集合</returns>
        public virtual async Task<List<T>> FindWithPagerAsync(string condition, PagerInfo info)
        {
            return await baseRepository.FindWithPagerAsync(condition, info);
        }
        /// <summary>
        /// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
        /// </summary>
        /// <param name="condition">查询的条件</param>
        /// <param name="info">分页实体</param>
        /// <param name="fieldToSort">排序字段</param>
        /// <returns>指定对象的集合</returns>
        public virtual async Task<List<T>> FindWithPagerAsync(string condition, PagerInfo info, string fieldToSort)
        {
            return await baseRepository.FindWithPagerAsync(condition, info, fieldToSort);
        }
        /// <summary>
        /// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
        /// </summary>
        /// <param name="condition">查询的条件</param>
        /// <param name="info">分页实体</param>
        /// <param name="fieldToSort">排序字段</param>
        /// <param name="desc">排序方式</param>
        /// <returns>指定对象的集合</returns>
        public virtual async Task<List<T>> FindWithPagerAsync(string condition, PagerInfo info, string fieldToSort, bool desc)
        {
            return await baseRepository.FindWithPagerAsync(condition, info, fieldToSort, desc);
        }
        /// <summary>
        /// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
        /// 查询条件变换时请重写该方法。
        /// </summary>
        /// <param name="search">查询的条件</param>
        /// <returns>指定对象的集合</returns>
        public virtual async Task<PageResult<TODto>> FindWithPagerAsync(SearchInputDto<T> search)
        {
            bool order = search.Order == "asc" ? false : true;
            PagerInfo pagerInfo = new PagerInfo
            {
                CurrenetPageIndex = search.CurrenetPageIndex,
                PageSize = search.PageSize
            };
            string where = string.Empty;
            List<T> list = await baseRepository.FindWithPagerAsync(where, pagerInfo, search.Sort, order);
            PageResult<TODto> pageResult = new PageResult<TODto>
            {
                CurrentPage = pagerInfo.CurrenetPageIndex,
                Items = list.MapTo<TODto>(),
                ItemsPerPage = pagerInfo.PageSize,
                TotalItems = pagerInfo.RecordCount
            };
            return pageResult;
        }
        #endregion 分页实体列表

        #region Count、Max、Min、Sum
        /// <summary>
        /// 异步根据条件统计数据（sql语句方法）
        /// </summary>
        /// <param name="whereSql">条件语句 如："Age=@Age"</param>
        /// <param name="parameters">条件参数 如：new { Age=30 }</param>
        /// <returns></returns>
        public virtual async Task<int> GetCountByWhereAsync(string whereSql = "", object? parameters = null)
        {
            return await baseRepository.GetCountByWhereAsync(whereSql, parameters);
        }
        /// <summary>
        /// 异步根据条件统计数据（表达式方法）
        /// </summary>
        /// <param name="whereExpression">查询条件表达式</param>
        /// <returns></returns>
        public virtual async Task<int> GetCountByWhereAsync(Expression<Func<T, bool>> whereExpression)
        {
            return await baseRepository.GetCountByWhereAsync(whereExpression);
        }
        /// <summary>
        /// 异步根据条件查询获取某个字段的最大值（sql语句方法）
        /// </summary>
        /// <typeparam name="TResult">最大值的数据类型</typeparam>
        /// <param name="maxField">最大值的字段名称</param>
        /// <param name="whereSql">条件语句 如："Age=@Age"</param>
        /// <param name="parameters">条件参数 如：new { Age=30 }</param>
        /// <returns>返回字段的最小值</returns>
        public virtual async Task<TResult> GetMaxValueByFieldAsync<TResult>(string maxField, string whereSql = "", object? parameters = null)
        {
            return await baseRepository.GetMaxValueByFieldAsync<TResult>(maxField, whereSql, parameters);
        }
        /// <summary>
        /// 异步根据条件查询获取某个字段的最大值（表达式方法）
        /// </summary>
        /// <typeparam name="TResult">最大值的数据类型</typeparam>
        /// <param name="maxFieldExpression">最大值字段表达式</param>
        /// <param name="whereExpression">查询条件表达式</param>
        /// <returns>返回字段的最小值</returns>
        public virtual async Task<TResult> GetMaxValueByFieldAsync<TResult>(Expression<Func<T, TResult>> maxFieldExpression, Expression<Func<T, bool>> whereExpression)
        {
            return await baseRepository.GetMaxValueByFieldAsync<TResult>(maxFieldExpression, whereExpression);
        }
        /// <summary>
        /// 异步根据条件查询获取某个字段的最小值（sql语句方法）
        /// </summary>
        /// <typeparam name="TResult">最小值的数据类型</typeparam>
        /// <param name="minField">最小值的字段名称</param>
        /// <param name="whereSql">条件语句 如："Age=@Age"</param>
        /// <param name="parameters">条件参数 如：new { Age=30 }</param>
        /// <returns>返回字段的最小值</returns>
        public virtual async Task<TResult> GetMinValueByFieldAsync<TResult>(string minField, string whereSql = "", object? parameters = null)
        {
            return await baseRepository.GetMinValueByFieldAsync<TResult>(minField, whereSql, parameters);
        }
        /// <summary>
        /// 异步根据条件查询获取某个字段的最小值（表达式方法）
        /// </summary>
        /// <typeparam name="TResult">最小值的数据类型</typeparam>
        /// <param name="minFieldExpression">最小值字段表达式</param>
        /// <param name="whereExpression">查询条件表达式</param>
        /// <returns>返回字段的最小值</returns>
        public virtual async Task<TResult> GetMinValueByFieldAsync<TResult>(Expression<Func<T, TResult>> minFieldExpression, Expression<Func<T, bool>> whereExpression)
        {
            return await baseRepository.GetMinValueByFieldAsync<TResult>(minFieldExpression, whereExpression);
        }
        /// <summary>
        /// 异步根据条件统计某个字段之和,sum(字段)（sql语句方法）
        /// </summary>
        /// <typeparam name="TResult">求和后的数据类型</typeparam>
        /// <param name="sumField">求和的字段名称</param>
        /// <param name="whereSql">条件语句 如："Age=@Age"</param>
        /// <param name="parameters">条件参数 如：new { Age=30 }</param>
        /// <returns></returns>
        public virtual async Task<TResult> GetSumValueByFieldAsync<TResult>(string sumField, string whereSql = "", object? parameters = null)
        {
            return await baseRepository.GetSumValueByFieldAsync<TResult>(sumField, whereSql, parameters);
        }
        /// <summary>
        /// 异步根据条件统计某个字段之和,sum(字段)（表达式方法）
        /// </summary>
        /// <typeparam name="TResult">求和后的数据类型</typeparam>
        /// <param name="sumFieldExpression">求和字段表达式</param>
        /// <param name="whereExpression">查询条件表达式</param>
        /// <returns></returns>
        public virtual async Task<TResult> GetSumValueByFieldAsync<TResult>(Expression<Func<T, TResult>> sumFieldExpression, Expression<Func<T, bool>> whereExpression)
        {
            return await baseRepository.GetSumValueByFieldAsync<TResult>(sumFieldExpression, whereExpression);
        }
        #endregion Count、Max、Min、Sum
        #endregion 异步查询

        #region 异步物理删除
        /// <summary>
        /// 异步按主键删除
        /// </summary>
        /// <param name="id">主键Id List集合</param>
        /// <returns>执行成功返回true，否则为false。</returns>
        public virtual async Task<bool> DeleteByIdAsync(long id)
        {
            return await baseRepository.DeleteByIdAsync(id);
        }
        /// <summary>
        /// 异步按主键批量删除
        /// </summary>
        /// <param name="ids">主键Id List集合</param>
        /// <returns>执行成功返回true，否则为false。</returns>
        public virtual async Task<bool> DeleteByIdsAsync(IEnumerable<long> ids)
        {
            return await baseRepository.DeleteByIdsAsync(ids);
        }
        /// <summary>
        /// 异步按条件删除
        /// </summary>
        /// <param name="whereSql">条件语句 如："Age > @Age"</param>
        /// <param name="parameters">条件参数及值 如：new { Age = 30 }</param>
        /// <returns></returns>
        public virtual async Task<bool> DeleteByWhereAsync(string whereSql = "", object? parameters = null)
        {
            return await baseRepository.DeleteByWhereAsync(whereSql, parameters);
        }
        #endregion

        #region 事务
        /// <summary>
        /// 异步多表多数据操作批量插入、更新、删除--事务
        /// </summary>
        /// <param name="trans">事务</param>
        /// <param name="commandTimeout">超时</param>
        /// <returns></returns>
        public virtual async Task<Tuple<bool, string>> ExecuteTransactionAsync(List<Tuple<string, object?>> trans, int? commandTimeout = null)
        {
            return await baseRepository.ExecuteTransactionAsync(trans, commandTimeout);
        }
        /// <summary>
        /// 异步使用事务
        /// </summary>
        /// <param name="action">事务执行的方法</param>
        /// <returns></returns>
        public async Task<DbResult<bool>> UseTranAsync(Func<Task> action)
        {
            return await baseRepository.UseTranAsync(action);
        }
        #endregion 事务
        #endregion 异步方法

        #region IDisposable Support
        private bool disposedValue = false; // 要检测冗余调用
        /// <summary>
        /// 
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)。
                }

                // TODO: 释放未托管的资源(未托管的对象)并在以下内容中替代终结器。
                // TODO: 将大型字段设置为 null。

                disposedValue = true;
            }
        }

        // TODO: 仅当以上 Dispose(bool disposing) 拥有用于释放未托管资源的代码时才替代终结器。
        // ~BaseService() {
        //   // 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。
        //   Dispose(false);
        // }

        /// <summary>
        /// 添加此代码以正确实现可处置模式
        /// </summary>
        void IDisposable.Dispose()
        {
            // 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。
            Dispose(true);
            // TODO: 如果在以上内容中替代了终结器，则取消注释以下行。
            // GC.SuppressFinalize(this);
        }

        #endregion
    }
}
